package com.ruoyi.web;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.ruoyi.system.Control.Record;
import com.ruoyi.system.domain.QService;
import com.ruoyi.system.service.IQServiceService;
import com.ruoyi.web.controller.common.QServicecj;
import com.ruoyi.web.controller.common.Teljilu;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Component

public class MyTask {

    @Autowired
    private IQServiceService iqServiceService;
    /**
     * 定时任务 每隔5秒触发一次
     */
  //  @Scheduled(cron = "0/5 * * * * ?")
    public void executeTask() throws IOException, UnirestException {

        Unirest.setTimeouts(0, 0);
        HttpResponse<String> response1 = Unirest.get("http://zeec.yuexunai.com/open/login?user.userName=chen&user.password=Qwe123.")
                .header("User-Agent", "Apifox/1.0.0 (https://apifox.com)")
                .header("Accept", "*/*")
                .header("Host", "zeec.yuexunai.com")
                .header("Connection", "keep-alive")
                .asString();

        // 输出响应内容
        System.out.println (response1.getBody());

        System.out.println("定时任务开始执行：获取每日通话记录" +  new Date());

        String currentDate = DateUtil.today(); // 获取当前日期的字符串形式
        System.out.println("当前日期为：" + currentDate);
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("text/plain");
        RequestBody body = RequestBody.create(mediaType, "");
        Request request = new Request.Builder()
                .url("http://zeec.yuexunai.com/getAgentHistoryScust_im?scust.servicetype=5&pagebean.pageNo=1&pagebean.pageSize=10&scust.begintime="+currentDate+"%2000:00:00&scust.endtime="+currentDate+"%2023:59:59&organId=1")
                .method("POST", body)
                .addHeader("Cookie", "sid=d28a4dfa-9b76-44ed-a09a-f2d2423949c6")
                .addHeader("User-Agent", "Apifox/1.0.0 (https://apifox.com)")
                .addHeader("Authorization", "Bearer 3baed334a1e3441fa126b862ddbdb65d")
                .addHeader("Accept", "*/*")
                .addHeader("Host", "zeec.yuexunai.com")
                .addHeader("Connection", "keep-alive")
                .build();
        Response response = client.newCall(request).execute();

        // 输出响应内容
        String json = (response.body().string());

        System.out.println(json) ;

        // 创建Gson对象
        Gson gson = new Gson();
        Teljilu teljilu = gson.fromJson(json, Teljilu.class);

        System.out.println(teljilu.getCode()) ;
        QService qService = new QService();

        for(QServicecj qServicecj : teljilu.getContent().getList()){

            UUID uuid = UUID.randomUUID();
            System.out.println(uuid.toString());
            qService.setVoxfile(qServicecj.getVoxfile());
            qService.setBeginTime(qServicecj.getBegintime());
            qService.setEndTime(qServicecj.getEndtime());
            qService.setFromnum(qServicecj.getFromnum());
            qService.setCname(qServicecj.getCname());
           // fromnum cname


            qService.setServicetype(qServicecj.getServicetype());
            qService.setServiceId(uuid.toString());



         //   if (iqServiceService.selectQServiceByServiceId(chatRecord.getChatSessionId()) == null) {

                iqServiceService.insertQService(qService);
          //  }else{
                //聊天次数
         //       iqServiceService.updateQService(qService);
         //   }


        }

    }
}